package com.ruoyi.order.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.app.Vo.CartItem;
import com.ruoyi.order.domain.app.Vo.OrderDetailsResultViewVo;
import com.ruoyi.order.domain.app.Vo.OrderViewVo;
import com.ruoyi.order.domain.app.Vo.UserAddr;
import com.ruoyi.order.mapper.OrderAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.OrderMapper;
import com.ruoyi.order.domain.Order;
import com.ruoyi.order.service.IOrderService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-04
 */
@Service
public class OrderServiceImpl implements IOrderService {
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderAddressMapper orderAddressMapper;
    /**
     * 查询订单
     *
     * @param saleID 订单主键
     * @return 订单
     */
    @Override
    public Order selectOrderBySaleID(Integer saleID) {
        return orderMapper.selectOrderBySaleID(saleID);
    }

    /**
     * 查询订单列表
     *
     * @param order 订单
     * @return 订单
     */
    @Override
    public List<Order> selectOrderList(Order order) {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    public int insertOrder(Order order) {
        order.setCreateTime(DateUtils.getNowDate());
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    public int updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单
     *
     * @param saleIDs 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderBySaleIDs(String saleIDs) {
        return orderMapper.deleteOrderBySaleIDs(Convert.toStrArray(saleIDs));
    }

    /**
     * 删除订单信息
     *
     * @param saleID 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderBySaleID(Integer saleID) {
        return orderMapper.deleteOrderBySaleID(saleID);
    }

    @Override
    public OrderDetailsResultViewVo getOrderInfoByOrderNO(String orderNumber) {
        OrderDetailsResultViewVo result = new OrderDetailsResultViewVo();
        Map<String,Object> map = new HashMap<>();
        map.put("orderNO",orderNumber);
        OrderDetailsResultViewVo list = orderMapper.getOrderInfoByOrderNO(orderNumber);
        if(list!=null){
            result = list;
            map.clear();
            map.put("orderNO",orderNumber);
            List<CartItem>  cartItemList = orderMapper.getOrderItemDtosByOrderNO(map);
            if(cartItemList!=null&&cartItemList.size()>0){
                result.setOrderItemDtos(cartItemList);
                map.clear();
                map.put("saleID",list.getSaleID());
                UserAddr address = orderAddressMapper.getAddress(map);
                result.setUserAddrDto(address);
            }
        }
        return result;
    }

    @Override
    public OrderViewVo getOrderPayInfoByOrderNumber(String orderNumber) {
        OrderViewVo result  = orderMapper.getOrderPayInfoByOrderNumber(orderNumber);
        if (result!=null){
            result.setProdNameList(orderMapper.getProductName(orderNumber));
        }
        return result;
    }
}
